/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import connection.ConnectionDB;
import java.sql.CallableStatement;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Car;
import model.CarBooking;

/**
 *
 * @author khanhanh
 */
public class CarDAL {

    private ConnectionDB conn = new ConnectionDB();
    private CallableStatement cs;
    private ResultSet rs;

    public Car getCarByID(int carID){
        Car car = null;
        try {
            cs = conn.createConnection().prepareCall("{call sp_GetCarByID(?)}");
            cs.setInt("CarID", carID);
            rs = cs.executeQuery();
            while (rs.next()) {
                car = new Car();
                car.setModelID(rs.getInt("ModelID"));
                car.setCarID(carID);
                car.setCarName(rs.getString("CarName"));
                car.setCapacity(rs.getInt("Capacity"));
                car.setDescription(rs.getString("Description"));
                car.setPrice(rs.getFloat("Price"));
                car.setCarImg(rs.getString("Images"));
            }

            return car;
        } catch (SQLException ex) {
            Logger.getLogger(CarDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return car;
    }

    public boolean createCarBooking(int carID,int customerID,boolean driver,Date pickUpDate,Date dropOffDate,int quantity,float totalPerCar){
        try {
            cs = conn.createConnection().prepareCall("{call sp_CreateCarBooking(?,?,?,?,?,?,?)}");
            cs.setInt("CarID", carID);
            cs.setInt("CustomerID", customerID);
            cs.setBoolean("Driver", driver);
            cs.setDate("PickupDate", pickUpDate);
            cs.setDate("DropoffDate", dropOffDate);
            cs.setInt("Quantity", quantity);
            cs.setFloat("TotalPrice", totalPerCar);

            if (cs.executeUpdate()==1) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(CarDAL.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }

    }

    public CarBooking getCarBookingByID(int carBookingID){
        CarBooking carBooking = null;
        try {
            cs = conn.createConnection().prepareCall("{call sp_GetCarBookingByID(?)}");
            cs.setInt("CarBookingID", carBookingID);
            rs = cs.executeQuery();
            while (rs.next()) {
                carBooking = new CarBooking();
                carBooking.setCarBookingID(carBookingID);
                carBooking.setCustomerID(rs.getInt("CustomerID"));
                carBooking.setCarID(rs.getInt("CarID"));
                carBooking.setIsDriver(rs.getBoolean("Driver"));
                carBooking.setPickUpDate(rs.getDate("PickupDate"));
                carBooking.setDropOffDate(rs.getDate("DropoffDate"));
                carBooking.setQuantity(rs.getInt("Quantity"));
                carBooking.setPrice(rs.getFloat("TotalPrice"));
                carBooking.setAvaiable(rs.getBoolean("Available"));
            }

            return carBooking;
        } catch (SQLException ex) {
            Logger.getLogger(CarDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return carBooking;
    }
}
